package hyung.jin.seo.util;


import hyung.jin.seo.model.NumberBasket;

import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
// util..util...
public class Utility {

	/**
	 * This class is designed Singleton Pattern and
	 * used with SingletonHolder for safer thread-handling
	 */
	private Utility(){}

	public static Utility getInstance()
	{
		return SingletonHolder.instance;
	}
	
	private static class SingletonHolder
	{
		static final Utility instance = new Utility();
	}

	public NumberBasket generateNumber(NumberBasket basket)
	{
		int iterCnt = 0;
		String[] income = basket.getNumbers();
		int[] tempRepo = new int[LottoConstants.NUMBER_BALL];
		// check how many selected numbers come from user and keep them into temp[]
		for(int i=0; i<LottoConstants.NUMBER_BALL; i++)
		{
			if((income[i]!=null)&&(!income[i].equals(LottoConstants.EMPTY_NUMBER)))
			{
				tempRepo[iterCnt] = Integer.parseInt(income[i].trim());
				iterCnt++;
			}
		}
		
		TreeSet<Integer> ts = new TreeSet<Integer>();
		Random num = new Random();
		int generated=0;
		int count=iterCnt;
		do
		{
//			generated = num.nextInt(45)+1;
			generated = num.nextInt(8)+1;
			// check if user already selects the same number
			if(isContained(tempRepo, generated))
			{
				continue;
			}
			if(ts.add(new Integer(generated)))
			{
				count++;
			}
		}while(count<6);
		
		// if there is any user defined number, add it into TreeSet for automatic sorting
		for(int j=0; j<LottoConstants.NUMBER_BALL; j++)
		{
			if((income[j]!=null)&&(!income[j].equals(LottoConstants.EMPTY_NUMBER)))
			{
				ts.add(new Integer(income[j].trim()));
			}
		}
		return getNumbers(ts, basket);
	}
	
	private boolean isContained(int[] tempRepo, int generated)
	{
		for(int i=0; i<LottoConstants.NUMBER_BALL; i++)
		{
			if(tempRepo[i]==generated)
			{
				return true;
			}
		}
		return false;
	}

	private NumberBasket getNumbers(Set<Integer> set, NumberBasket basket)
	{
		if(set==null)
		{
			return new NumberBasket(new String[]{"0","0","0","0","0","0"}, new boolean[]{false, false, false, false, false, false});
		}
		String[] tempNum = basket.getNumbers();
		Object[] integer = (Object[]) set.toArray();
		String[] selectedNum = new String[LottoConstants.NUMBER_BALL];
		for(int i=0; i<LottoConstants.NUMBER_BALL; i++)
		{
			selectedNum[i] = integer[i].toString();
		}
		// check index of temp[] element so as to put boolean[] value into basket
		boolean[] choice = new boolean[LottoConstants.NUMBER_BALL];
		first:for(int j=0; j<LottoConstants.NUMBER_BALL; j++)
		{
			if((tempNum[j]!=null)&&(!tempNum[j].equals(LottoConstants.EMPTY_NUMBER))) // if number is there
			{
				// find index of number in result
				second:for(int k=0; k<LottoConstants.NUMBER_BALL; k++)
				{
					if(selectedNum[k].trim().equals(tempNum[j].trim()))
					{
						// set true into boolean value in basket
						choice[k] = true;
						break second;
					}
				}
			}
		}
		basket.setNumbers(selectedNum);
		basket.setSelected(choice);
		return basket;
	}
	
}
